<?php

/**
 * Used to create CSS files from '.less' files. See the LESS project at: lesscss.org
 *  and see the PHP compiler for it at: leafo.net/lessphp 
 *
 */
class LessCssHelper extends AppHelper {
  
  var $helpers = array('Html');
  
  
  function __construct($options = null) {
  
    parent::__construct($options);
    
  } ///////////// end constructor /////////////////
  
  
  /**
   * Compile the 'Less CSS' file $lessFile into a CSS file, save it, and return an Html <link> that 
   *  can be included in an Html <head> to include the CSS file for a page.  It will write the new CSS
   *  file in the same dir has the $lessFile.  It only compiles the 'less' file if it is newer than 
   *  the output CSS file.
   * 
   * @param string $lessFile  Name of the ".less" file. The path is assumed to be relative to 'webroot/css'.
   *                          For example, if the '.less' is in 'webroot/css/myLib/my_file.less', then you
   *                          would pass $lessFile as "myLib/my_file.less".
   */
  function cssLinkFromLess($lessFile) {
    
    // For the import rules see the example that loads "vendors/services/well.named.php" at:
    //   http://book.cakephp.org/view/944/Vendor-examples
    $importSuccess = App::import('Vendor', 'LesscIncPhp', array('file'=>'less_php'.DS.'lessc.inc.php'));
    
    // gives something like D:\bla\bla\bla\imagerecon\
    $appFullPath = GlobalsClass::getImgReconPath();
    //die("Path to app: $appFullPath" ."<br/>   Path to webroot: $this->webroot");
   
    // get info about the file (path, extension, etc) so we can create other paths
    $lessFileInfo = pathinfo($lessFile);
    $lessFileWithoutExtension = '';
    if(isset($lessFileInfo['extension'])) {
      // get name part only (without any path), and also cut off any extention
      $lessFileWithoutExtension = basename($lessFile, '.' . $lessFileInfo['extension']); 
    }
    else {
      // get name part only (without any path)
      $lessFileWithoutExtension = basename($lessFile);
    }
    
    
    $lessFilePathRelToCss = dirname($lessFile); // assumes $lessFile contains path relative to webroot/css
    if($lessFilePathRelToCss == ".") {
      $lessFilePathRelToCss = "";
    }
    
    //die("less file without extension: $lessFileWithoutExtension "
    //        ."<br/> lessFilePathRelToCss: $lessFilePathRelToCss");
    
    $cssDirFullPath = $appFullPath . "webroot" . DS . "css"; 
    $cssFileFullPath = '';
    $cssFileNameWithExt = $lessFileWithoutExtension . ".css";
    if($lessFilePathRelToCss) {
      $cssFileFullPath = $cssDirFullPath .DS. $lessFilePathRelToCss .DS. $cssFileNameWithExt;
    }
    else {
      $cssFileFullPath = $cssDirFullPath .DS. $cssFileNameWithExt;
    }    

    $lessFileFullPath = $cssDirFullPath .DS. $lessFile;
    
        //pr("Extention is: {$lessFileInfo['extension']}");
    
    // If the less filename passed in didn't have the extension ".less", add it now.
        //if( $lessFileInfo['extension']  == '') {
    if( !isset($lessFileInfo['extension']) || $lessFileInfo['extension']  == '') {
      $lessFileFullPath .= ".less";
    }
    
        //die("css file full path: $cssFileFullPath <br/> less file full path: $lessFileFullPath");
    
    // Finally, compile and save the ".less" file to a ".css" file
    try {
      lessc::ccompile($lessFileFullPath , $cssFileFullPath);
    } 
    catch (exception $ex) {
      exit('lessc fatal error:<br />'.$ex->getMessage());
    }
    
    // return an html <link> element for the css file 
    
    $cssFileRelPath = '';
    if($lessFilePathRelToCss) {
      $cssFileRelPath = $lessFilePathRelToCss .DS. $cssFileNameWithExt;
    }
    else {
      $cssFileRelPath = $cssFileNameWithExt;
    }
    //die("cssFileRelPath: $cssFileRelPath");
    return $this->Html->css($cssFileRelPath);
    
  } ////////// end cssLinkFromLess() //////////////////
  
  

} //////////////////////// end class ////////////////////////////////

?>
